﻿using Common;
using System;
using System.Windows;
using T68Control.Procdure;

namespace T68Control.ViewModel
{
    internal class UserEditViewModel : NotifyBase
    {
        public LoginModel LoginModel { get; set; } = new LoginModel();
        private string _errorMessage = string.Empty;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; this.DoNotify(); }
        }

        public UserEditViewModel()
        {
            this.CloseWindowCommand = new CommandBase();
            this.CloseWindowCommand.DoExecute = new Action<object>((o) =>
            {
                (o as Window).Close();
            });
            SaveUserCommand = new CommandBase(SaveUser);

            LoginModel.UserLevel = UserLevel.Operator;
            LoginModel.UserName = "";
            LoginModel.Password = "";
        }

        private void SaveUser(object obj)
        {
            if (string.IsNullOrEmpty(LoginModel.UserName))
            {
                this.ErrorMessage = "用户名不能为空！";
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.Password))
            {
                this.ErrorMessage = "密码不能为空！";
                return;
            }
            var user = new UserEntity
            {
                UserName = LoginModel.UserName,
                Password = LoginModel.Password,
                UserLevel = LoginModel.UserLevel,
            };
            Global.EditUser = user;
            var win = obj as Window;
            win.DialogResult = true;
        }

        public CommandBase SaveUserCommand { get; set; }
        public CommandBase CloseWindowCommand { get; set; }
    }
}